//
//  JTTopicPictureView.m
//  百思不得姐
//
//  Created by Robin Zhang on 2017/1/13.
//  Copyright © 2017年 BeiJingWangDianJieTong. All rights reserved.
//

#import "JTTopicPictureView.h"
#import <UIImageView+WebCache.h>
#import "JTTopic.h"
#import <AFNetworking.h>
#import <DACircularProgress/DALabeledCircularProgressView.h>
#import "JTSeeBigPictureViewController.h"

@interface JTTopicPictureView ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView *gifImageView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigButton;
@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *progressView;

@end

@implementation JTTopicPictureView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 从xib中加载进来的空间的autoresizingMask默认是 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ，需要清空
    [self setAutoresizingMask:UIViewAutoresizingNone];
    
    // 设置progressView的圆角
    [self.progressView setRoundedCorners:5];
    
    // 给imageView添加点击手势
    [self.imageView setUserInteractionEnabled:YES];
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seeBig)]];
    
    // 给seeBigButton添加点击事件
    [self.seeBigButton addTarget:self action:@selector(seeBig) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setTopic:(JTTopic *)topic {
    
    _topic = topic;
    
    /** 0.显示进度条 */
    [self.progressView setHidden:NO];
    
    /** 1.设置图片 */
    NSString *url = topic.largeImage;
#if TARGET_IPHONE_SIMULATOR // 当前设备是模拟器(显示大图)
#elif TARGET_OS_IPHONE // 真机
    
    AFNetworkReachabilityStatus status = [[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus];
    
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络(小图)
            url = topic.smallImage;
            break;
            
        case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI(大图)
            break;
            
        case AFNetworkReachabilityStatusUnknown | AFNetworkReachabilityStatusNotReachable: // 网络有问题，清空当前显示的图片
            [self.imageView setImage:nil];
            url = nil;
            break;
            
        default:
            break;
    }
#endif
    
    if (url.length) {
        
        JTWeak(self)
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            // receivedSize:已经下载了多少
            // expectedSize:图片的总大小
            // 当前下载的百分比
            CGFloat progress = 1.0 * receivedSize / expectedSize;
            [weak_self.progressView setProgress:progress];
            [weak_self.progressView.progressLabel setText:[NSString stringWithFormat:@"%.0f%%", progress * 100]];
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            /** 1.隐藏进度条 */
            [weak_self.progressView setHidden:YES];
            
            /** 2.如果不是gif动图，等比例缩放图片至imageView的宽度 */
            if (!topic.is_gif) [weak_self.imageView setImage:[image jt_imageCompressWithTargetScaledWidth:weak_self.imageView.jt_width]];
        }];
    }
    
    /** 2.隐藏与显示GIF角标 */
    [self.gifImageView setHidden:!topic.is_gif];
    
    /** 3.隐藏与显示“点击查看大图”按钮 以及 改变imageView的contentMode以保证图片不变形 */
    [self.seeBigButton setHidden:!topic.isBigPicure];
    if (topic.isBigPicure) {
        
        [self.imageView setContentMode:UIViewContentModeTop];
    }
    else {
        
        [self.imageView setContentMode:UIViewContentModeScaleToFill];
    }
    [self.imageView setClipsToBounds:topic.isBigPicure];
}

#pragma mark - 监听
- (void)seeBig {
    
    JTSeeBigPictureViewController *viewController = [[JTSeeBigPictureViewController alloc] init];
    [viewController setTopic:self.topic];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:viewController animated:YES completion:nil];
}

@end
